1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.annotations.GwtCompatible;
21  
22  /**
23   * A time source; returns a time value representing the number of nanoseconds elapsed since some
24   * fixed but arbitrary point in time. Note that most users should use {@link Stopwatch} instead of
25   * interacting with this class directly.
26   *
27   * <p><b>Warning:</b> this interface can only be used to measure elapsed time, not wall time.
28   *
29   * @author Kevin Bourrillion
30   * @since 10.0
31   *     (<a href="http://code.google.com/p/guava-libraries/wiki/Compatibility"
32   *     >mostly source-compatible</a> since 9.0)
33   */
34  @Beta
35  @GwtCompatible
36  public abstract class Ticker {
37    /**
38     * Constructor for use by subclasses.
39     */
40    protected Ticker() {}
41  
42    /**
43     * Returns the number of nanoseconds elapsed since this ticker's fixed
44     * point of reference.
45     */
46    public abstract long read();
47  
48    /**
49     * A ticker that reads the current time using {@link System#nanoTime}.
50     *
51     * @since 10.0
52     */
53    public static Ticker systemTicker() {
54      return SYSTEM_TICKER;
55    }
56  
57    private static final Ticker SYSTEM_TICKER = new Ticker() {
58      @Override
59      public long read() {
60        return Platform.systemNanoTime();
61      }
62    };
63  }